/*
 * 判断整数序列是不是二元查找树的后序遍历结果
 *
 */

#include <stdio.h>
#include <stdlib.h>

bool is_after_index_result(int* array, int start, int end)
{
    if (start == end) {
        return true;
    }
    int i = start;
    while (array[i] < array[end] && i < end) {
        i++;
    }
    int j = i;
    while(array[j+1] > array[end] && j < end) {
        j++;
    }
    if (j != end-1) {
        return false;
    }
    else {
        if (false == is_after_index_result(array, start, i-1))
            return false;
        if (false == is_after_index_result(array, i, end-1))
            return false;
        return true;
    }
}


int main()
{
    int array[] = {5, 7, 6, 9, 11, 10, 8};
    if (true == is_after_index_result(array, 0, 6)) {
        printf("good\n");
    }
    else {
        printf("bad\n");
    }
}
